package com.front.web.lvyou_specialty_front.util;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.CurrencyUtils;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyOrderContent;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyProduct;
import com.front.web.lvyou_specialty_front.pagevo.GoodsContentBodyVo;
import com.front.web.lvyou_specialty_front.pagevo.GoodsContentVo;
import org.apache.commons.lang3.concurrent.ConcurrentUtils;

import java.math.BigDecimal;
import java.util.*;

public class GoodsContentUtil {

    public static Map<String, Object> createContentBodyList(List<GoodsContentBodyVo> resultList, List<GoodsContentVo> list, boolean isMember) {
        // 建立id集合
        Set<Long> merchantIdSet = new HashSet<>();
        Map<String, GoodsContentBodyVo> merchantMap = new HashMap<>();

        for (GoodsContentVo one: list) {
            if (one.getGoodsStatus() == 1) {
                continue;
            }
            if (merchantIdSet.contains(one.getMerchantId())) {
                GoodsContentBodyVo goodsContentBodyVo = merchantMap.get("" + one.getMerchantId());

                goodsContentBodyVo.getOrderContentList().add(one);

                if (isMember) {
                    goodsContentBodyVo.setGoodsFee(CurrencyUtils.add(new BigDecimal(goodsContentBodyVo.getGoodsFee()), CurrencyUtils.multiply(new BigDecimal(one.getGoodsHprice()), new BigDecimal(one.getGoodsNumber()))).doubleValue());
                } else {
                    goodsContentBodyVo.setGoodsFee(CurrencyUtils.add(new BigDecimal(goodsContentBodyVo.getGoodsFee()), CurrencyUtils.multiply(new BigDecimal(one.getGoodsPrice()), new BigDecimal(one.getGoodsNumber()))).doubleValue());
                }
                if (one.getFreight().longValue() > goodsContentBodyVo.getFreight()) {
                    goodsContentBodyVo.setOneMerchantFee(CurrencyUtils.add(new BigDecimal(one.getFreight()), new BigDecimal(goodsContentBodyVo.getGoodsFee())).doubleValue());
                    goodsContentBodyVo.setFreight(one.getFreight());
                } else {
                    goodsContentBodyVo.setOneMerchantFee(CurrencyUtils.add(new BigDecimal(goodsContentBodyVo.getFreight()), new BigDecimal(goodsContentBodyVo.getGoodsFee())).doubleValue());

                }

                goodsContentBodyVo.setOneMerchantNumber(goodsContentBodyVo.getOneMerchantNumber() + one.getGoodsNumber());

                merchantMap.put(one.getMerchantId() + "", goodsContentBodyVo);
            } else {
                merchantIdSet.add(one.getMerchantId());
                GoodsContentBodyVo goodsContentBodyVo = new GoodsContentBodyVo();
                goodsContentBodyVo.setOne(true);
                goodsContentBodyVo.setMerchantId(one.getMerchantId());
                goodsContentBodyVo.setMerchantName(one.getMerchantName());
                List<GoodsContentVo> list2 = new ArrayList<>();
                list2.add(one);
                goodsContentBodyVo.setOrderContentList(list2);
                goodsContentBodyVo.setFreight(one.getFreight());

                BigDecimal hFee = CurrencyUtils.multiply(new BigDecimal(one.getGoodsHprice()), new BigDecimal(one.getGoodsNumber()));
                BigDecimal yuanFee = CurrencyUtils.multiply(new BigDecimal(one.getGoodsPrice()), new BigDecimal(one.getGoodsNumber()));
                if (isMember) {
                    goodsContentBodyVo.setGoodsFee(hFee.doubleValue());
                } else {
                    goodsContentBodyVo.setGoodsFee(yuanFee.doubleValue());
                }
                goodsContentBodyVo.setMinusFee(CurrencyUtils.minus(yuanFee, hFee).doubleValue());
                goodsContentBodyVo.setOneMerchantFee(CurrencyUtils.add(new BigDecimal(goodsContentBodyVo.getFreight()), new BigDecimal(goodsContentBodyVo.getGoodsFee())).doubleValue());
                goodsContentBodyVo.setOneMerchantNumber(one.getGoodsNumber());
                merchantMap.put(one.getMerchantId()+ "", goodsContentBodyVo);
            }
        }

        Map<String, Object> resultMap = new HashMap<>();
        BigDecimal total = new BigDecimal(0);
        for (Map.Entry one: merchantMap.entrySet()) {
            GoodsContentBodyVo bodyVo = (GoodsContentBodyVo) one.getValue();
            total = CurrencyUtils.add(total, new BigDecimal(bodyVo.getOneMerchantFee()));
            resultList.add(bodyVo);
        }
        resultList.sort(new Comparator<GoodsContentBodyVo>() {
            @Override
            public int compare(GoodsContentBodyVo o1, GoodsContentBodyVo o2) {
                if (o1.getMerchantId() == null || o2.getMerchantId() == null) {
                    return 0;
                }
                return o1.getMerchantId().intValue() - o2.getMerchantId().intValue();
            }
        });
        resultMap.put("totalFee", total.doubleValue());

        return resultMap;
    }

    public static void createContentBodyListFrom(List<GoodsContentBodyVo> resultList, List<LvyouSpecialtyOrderContent> contentList) {

        Map<String, GoodsContentBodyVo> merchantMap = new HashMap<>();

        for (LvyouSpecialtyOrderContent one: contentList) {
            if (merchantMap.containsKey(one.getMerchantId() + "")) {
                GoodsContentBodyVo bodyVo = merchantMap.get(one.getMerchantId() + "");
                if (one.getFreight() > bodyVo.getFreight()) {
                    bodyVo.setFreight(one.getFreight());
                }
                bodyVo.getOrderContents().add(one);
                bodyVo.setOneMerchantFee(CurrencyUtils.add(new BigDecimal(bodyVo.getOneMerchantFee()), new BigDecimal(one.getRealFee())).doubleValue());
                merchantMap.put(one.getMerchantId() + "", bodyVo);
            } else {
                GoodsContentBodyVo bodyVo = new GoodsContentBodyVo();

                bodyVo.setMerchantId(one.getMerchantId());
                bodyVo.setMerchantName(one.getMerchantName());
                bodyVo.setFreight(one.getFreight());
                bodyVo.setGoodsFee(one.getGoodsFee());
                bodyVo.setOneMerchantFee(one.getRealFee());
                bodyVo.setMerchantOrderNo(one.getMerchantOrderNo());
                List<LvyouSpecialtyOrderContent> list = new ArrayList<>();
                list.add(one);
                bodyVo.setOrderContents(list);
                merchantMap.put(one.getMerchantId() + "", bodyVo);
            }
        }

        for (Map.Entry one: merchantMap.entrySet()) {
            GoodsContentBodyVo bodyVo = (GoodsContentBodyVo) one.getValue();
            resultList.add(bodyVo);
        }

        resultList.sort(new Comparator<GoodsContentBodyVo>() {
            @Override
            public int compare(GoodsContentBodyVo o1, GoodsContentBodyVo o2) {
                if (o1.getMerchantId() == null || o2.getMerchantId() == null) {
                    return 0;
                }
                return o1.getMerchantId().intValue() - o2.getMerchantId().intValue();
            }
        });

    }

    private static GoodsContentVo creatOneGoodsContentVo(LvyouSpecialtyProduct lvyouSpecialtyProduct) {
        GoodsContentVo goodsContentVo = new GoodsContentVo();
        goodsContentVo.setGoodsStatus(lvyouSpecialtyProduct.getStatus());
        goodsContentVo.setGoodsId(lvyouSpecialtyProduct.getId());

        goodsContentVo.setGoodsStandard(lvyouSpecialtyProduct.getStandard());
        goodsContentVo.setGoodsImage(lvyouSpecialtyProduct.getCover());
        goodsContentVo.setGoodsPrice(lvyouSpecialtyProduct.getPrice());
        goodsContentVo.setGoodsHprice(lvyouSpecialtyProduct.getHprice());
        goodsContentVo.setGoodsName(lvyouSpecialtyProduct.getName());
        goodsContentVo.setMerchantId(lvyouSpecialtyProduct.getMerchantId());
        goodsContentVo.setMerchantName(lvyouSpecialtyProduct.getMerchantName());
        goodsContentVo.setGoodsStatus(0);
        goodsContentVo.setRepotory(lvyouSpecialtyProduct.getReportory());
        goodsContentVo.setFreight(lvyouSpecialtyProduct.getFreight());

        return goodsContentVo;
    }

//    private static GoodsContentVo creatOneGoodsContentVoFrom (LvyouSpecialtyOrderContent lvyouSpecialtyOrderContent) {
//        GoodsContentVo goodsContentVo = new GoodsContentVo();
//        goodsContentVo.setGoodsId(lvyouSpecialtyOrderContent.getGoodsId());
//
//        goodsContentVo.setGoodsImage(lvyouSpecialtyOrderContent.getGoodsImages());
//        goodsContentVo.setGoodsPrice(lvyouSpecialtyOrderContent.getGoodsPrice());
//        goodsContentVo.setGoodsHprice(lvyouSpecialtyOrderContent.getGoodsHprice());
//        goodsContentVo.setGoodsName(lvyouSpecialtyOrderContent.getGoodsName());
//        goodsContentVo.setMerchantId(lvyouSpecialtyOrderContent.getMerchantId());
//        goodsContentVo.setMerchantName(lvyouSpecialtyOrderContent.getMerchantName());
//        goodsContentVo.setGoodsStatus(0);
//        goodsContentVo.setFreight(lvyouSpecialtyOrderContent.getFreight());
//
//        return goodsContentVo;
//    }

    public static void createFromProduct(List<GoodsContentVo> contentVoList, List<LvyouSpecialtyProduct> lvyouSpecialtyProducts, Map<String, Object> redisMap) {

        for (LvyouSpecialtyProduct one: lvyouSpecialtyProducts) {
            GoodsContentVo goodsContentVo = creatOneGoodsContentVo(one);
            Integer redisNumber = (Integer) redisMap.get(""+ one.getId());
            if (redisNumber > goodsContentVo.getRepotory()) {
                goodsContentVo.setGoodsStatus(1);
                goodsContentVo.setGoodsNumber(0);
            }  else {
                goodsContentVo.setGoodsNumber(redisNumber);
            }

            contentVoList.add(goodsContentVo);
        }
    }

}
